# 异或神经网络BP
import tensorflow as tf
import matplotlib.pyplot as plt
tf.set_random_seed(777)  #设置随机种子
learning_rate = 0.1
# 定义数据集
x_data = [[0, 0],
          [0, 1],
          [1, 0],
          [1, 1]]
y_data = [[0],
          [1],
          [1],
          [0]]
#定义占位符
X = tf.placeholder(tf.float32, [None, 2])
Y = tf.placeholder(tf.float32, [None, 1])
#模型和前向传播
W1 = tf.Variable(tf.random_normal([2, 3]), name='weight1')
b1 = tf.Variable(tf.random_normal([3]), name='bias1')
a1 = tf.sigmoid(tf.matmul(X, W1) + b1)

W2 = tf.Variable(tf.random_normal([3, 1]), name='weight2')
b2 = tf.Variable(tf.random_normal([1]), name='bias2')
a2 = tf.sigmoid(tf.matmul(a1, W2) + b2)
# 代价或损失函数
cost = -tf.reduce_mean(Y * tf.log(a2) + (1 - Y) * tf.log(1 - a2))
cost_history = [] # 损失值列表
#BP反向传播
#第2层
dz2 = a2 - Y
dW2 = tf.matmul(tf.transpose(a1), dz2) / tf.cast(tf.shape(a1)[0], dtype=tf.float32)
# dW2 = tf.matmul(tf.transpose(a1), dz2) /tf.shape(a1)[0]
db2 = tf.reduce_mean(dz2, axis=0)
#第1层
da1 = tf.matmul(dz2, tf.transpose(W2))
dz1 = da1 * a1 * (1 - a1)#m,3
dW1 = tf.matmul(tf.transpose(X), dz1) / tf.cast(tf.shape(X)[0], dtype=tf.float32)
db1 = tf.reduce_mean(dz1, axis=0)
# 参数更新
update = [
  tf.assign(W2, W2 - learning_rate * dW2),#w2 = w2 - learning_rate *dw2
  tf.assign(b2, b2 - learning_rate * db2),
  tf.assign(W1, W1 - learning_rate * dW1),
  tf.assign(b1, b1 - learning_rate * db1)
]
# 准确率计算
predicted = tf.cast(a2 > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
# 创建会话
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer()) #全局变量初始化
    # 迭代训练
    for step in range(3001):
        _, cost_val, acc_val = sess.run([update, cost, accuracy], feed_dict={X: x_data, Y: y_data})
        if step % 100 == 0:# 显示损失值收敛情况
            print(step, "Cost: ", cost_val, acc_val)
            cost_history.append(cost_val)
    h, c, a = sess.run([a2, predicted, accuracy], feed_dict={X: x_data, Y: y_data})
    print("\nHypothesis: \n", h, "\nCorrect: \n", c, "\nAccuracy: \n", a)
    # 画学习曲线
    plt.plot(cost_history[1: len(cost_history)])
    plt.show()
'''0 Cost:  0.77456033 0.5
100 Cost:  0.7049171 0.25
200 Cost:  0.7006392 0.25
300 Cost:  0.69759154 0.5
400 Cost:  0.6951836 0.5
500 Cost:  0.69307995 0.5
600 Cost:  0.6910514 0.5
700 Cost:  0.6889085 0.5
800 Cost:  0.68646365 0.5
900 Cost:  0.6835077 0.75
1000 Cost:  0.679796 0.75
1100 Cost:  0.6750514 0.75
1200 Cost:  0.6689877 0.75
1300 Cost:  0.66135323 0.75
1400 Cost:  0.6519719 0.75
1500 Cost:  0.64076996 0.75
1600 Cost:  0.6277964 0.75
1700 Cost:  0.6132252 0.75
1800 Cost:  0.59730875 0.75
1900 Cost:  0.58028805 0.75
2000 Cost:  0.5623032 0.75
2100 Cost:  0.543342 0.75
2200 Cost:  0.52323884 0.75
2300 Cost:  0.50170696 0.75
2400 Cost:  0.47838193 0.75
2500 Cost:  0.45285425 0.75
2600 Cost:  0.4247002 1.0
2700 Cost:  0.39357784 1.0
2800 Cost:  0.35949847 1.0
2900 Cost:  0.3232265 1.0
3000 Cost:  0.2864202 1.0

Hypothesis: 
 [[0.22781855]
 [0.86479574]
 [0.64230984]
 [0.2575084 ]] 
Correct: 
 [[0.]
 [1.]
 [1.]
 [0.]] 
Accuracy: 
 1.0
'''
